package com.wthink.hzgj.msgList

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import com.wthink.card.models.GMBInfoBundle
import com.wthink.hzgj.R
import com.wthink.hzgj.copyFile.callback.DirectoryIsEmpty
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.hzgj.databinding.FragmentMsgListBinding
import com.wthink.hzgj.databinding.FragmentSdcardBinding
import com.wthink.hzgj.msgList.`interface`.MsgCallback
import com.wthink.hzgj.msgList.viewmodel.MsgListViewmodel
import com.wthink.hzgj.util.PreferencesUtils


class MsgLisgFragment :Fragment(){
    private val viewModel by viewModels<MsgListViewmodel>()

    private lateinit var binding : FragmentMsgListBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_msg_list,
            container,
            false
        )

        binding.viewModel = viewModel
        binding.lifecycleOwner = this

/*
        viewModel.setMsg1Callback(object : MsgCallback {
            override fun msgCallback(noticeMsg: NoticeMsg) {
                if (noticeMsg.index.equals("-99")) {
                    binding.message.setText("无更多历史消息")
                }
            }
        })
*/

        viewModel.start()

        //var stringToList  = arrayListOf<String>("哈哈哈0","哈哈哈1","哈哈哈2","哈哈哈3","哈哈哈4","哈哈哈5","哈哈哈6","哈哈哈7")
        var stringToList  =  PreferencesUtils.stringToList()

        val bundle = arguments?.getInt("position")
        if (bundle != null) {
            GMBInfoBundle.setMsgInfoIndex(bundle)
            viewModel.msgDetail00.postValue(stringToList[bundle])
        }
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }

        binding.messagePrevious.setOnClickListener{
            viewModel.previous()
        }

        binding.messageNext.setOnClickListener{
            viewModel.next()
        }

        binding.messageDeleteOne.setOnClickListener{
            viewModel.deleteOne()
        }

        binding.messageDeleteAll.setOnClickListener{
            viewModel.deleteAll()
        }

        binding.messageList.setOnClickListener {
            Navigation.findNavController(it).navigate(R.id.msgListItemFragment)
        }
    }
}